home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox_gtk / hyper_text_view.py < prev   
Encoding:
Python Source  |  2009-04-27  |  4.5 KB  |  117 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import gtk
  20. import gobject
  21. import pango
  22.  
  23.  
  24. class HyperTextView(gtk.TextView):
  25.     __gtype_name__ = "HyperTextView"
  26.     __gsignals__ = {"anchor-clicked": (gobject.SIGNAL_RUN_LAST, None, (str, str, int))}
  27.     __gproperties__ = {
  28.         "link":  (gobject.TYPE_PYOBJECT, "link color", "link color of TextView", gobject.PARAM_READWRITE),
  29.         "active":(gobject.TYPE_PYOBJECT, "active color", "active color of TextView", gobject.PARAM_READWRITE),
  30.         "hover": (gobject.TYPE_PYOBJECT, "link:hover color", "link:hover color of TextView", gobject.PARAM_READWRITE),
  31.         }
  32.  
  33.     def do_get_property(self, prop):
  34.         try:
  35.             return getattr(self, prop.name)
  36.         except AttributeError:
  37.             raise AttributeError, "unknown property %s" % prop.name
  38.  
  39.     def do_set_property(self, prop, val):
  40.         if prop.name in self.__gproperties__.keys():
  41.             setattr(self, prop.name, val)
  42.         else:
  43.             raise AttributeError, "unknown property %s" % prop.name
  44.  
  45.     def __init__(self, buffer=None):
  46.         super(HyperTextView, self).__init__(buffer)
  47.         self.link   = {"foreground": "blue", "underline": pango.UNDERLINE_SINGLE}
  48.         self.active = {"foreground": "red", "underline": pango.UNDERLINE_SINGLE}
  49.         self.hover  = {"foreground": "dark blue", "underline": pango.UNDERLINE_SINGLE}
  50.  
  51.         self.set_editable(False)
  52.         self.set_cursor_visible(False)
  53.  
  54.         self.__tags = []
  55.  
  56.         self.connect("motion-notify-event", self._motion)
  57.         self.connect("focus-out-event", lambda w, e: self.get_buffer().get_tag_table().foreach(self.__tag_reset, e.window))
  58.  
  59.     def insert(self, text, _iter=None):
  60.         b = self.get_buffer()
  61.         if _iter is None:
  62.             _iter = b.get_end_iter()
  63.         b.insert(_iter, text)
  64.  
  65.     def insert_with_anchor(self, text, anchor=None, _iter=None):
  66.         b = self.get_buffer()
  67.         if _iter is None:
  68.             _iter = b.get_end_iter()
  69.         if anchor is None:
  70.             anchor = text
  71.  
  72.         tag = b.create_tag(None, **self.get_property("link"))
  73.         tag.set_data("is_anchor", True)
  74.         tag.connect("event", self._tag_event, text, anchor)
  75.         self.__tags.append(tag)
  76.         b.insert_with_tags(_iter, text, tag)
  77.  
  78.     def _motion(self, view, ev):
  79.         window = ev.window
  80.         x, y, _ = window.get_pointer()
  81.         x, y = view.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, x, y)
  82.         tags = view.get_iter_at_location(x, y).get_tags()
  83.         for tag in tags:
  84.             if tag.get_data("is_anchor"):
  85.                 for t in set(self.__tags) - set([tag]):
  86.                     self.__tag_reset(t, window)
  87.                 self.__set_anchor(window, tag, gtk.gdk.Cursor(gtk.gdk.HAND2), self.get_property("hover"))
  88.                 break
  89.         else:
  90.             tag_table = self.get_buffer().get_tag_table()
  91.             tag_table.foreach(self.__tag_reset, window)
  92.  
  93.     def _tag_event(self, tag, view, ev, _iter, text, anchor):
  94.         _type = ev.type
  95.         if _type == gtk.gdk.MOTION_NOTIFY:
  96.             return
  97.         elif _type in [gtk.gdk.BUTTON_PRESS, gtk.gdk.BUTTON_RELEASE]:
  98.             button = ev.button
  99.             cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
  100.             if _type == gtk.gdk.BUTTON_RELEASE:
  101.                 self.emit("anchor-clicked", text, anchor, button)
  102.                 self.__set_anchor(ev.window, tag, cursor, self.get_property("hover"))
  103.             elif button in [1, 2]:
  104.                 self.__set_anchor(ev.window, tag, cursor, self.get_property("active"))
  105.  
  106.     def __tag_reset(self, tag, window):
  107.         if tag.get_data("is_anchor"):
  108.             self.__set_anchor(window, tag, None, self.get_property("link"))
  109.  
  110.     def __set_anchor(self, window, tag, cursor, prop):
  111.         window.set_cursor(cursor)
  112.         for key, val in prop.iteritems():
  113.             if val is not None:
  114.                 tag.set_property(key, val)
  115.  
  116. gobject.type_register(HyperTextView)
  117.